package com.whmall.service.crm.provider;

import cn.hutool.core.collection.CollUtil;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.api.SupplierChemicalCoaApi;
import com.whmall.component.entity.dto.CompanyKeyFieldDTO;
import com.whmall.service.crm.entity.dto.company.CompanyQueryRspDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.ChemicalCoaInfoReqDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalCoaQueryByReqDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalCoaQueryReqDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalCoaQueryRspDTO;
import com.whmall.service.crm.module.company.service.CompanyService;
import com.whmall.service.crm.module.supplierchemical.service.SupplierChemicalCoaService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Description TODO
 * @Author yangkuan
 * Date 2020/9/19 11:33
 **/
@Service(interfaceClass = SupplierChemicalCoaApi.class)
public class SupplierChemicalCoaProvider implements SupplierChemicalCoaApi {

    @Autowired
    private SupplierChemicalCoaService supplierChemicalCoaService;

    @Autowired
    private CompanyService companyService;


    @Override
    public PageRspDTO<SupplierChemicalCoaQueryRspDTO> queryAllCoaInfo(SupplierChemicalCoaQueryReqDTO supplierChemicalCoaQueryReqDTO) {

        PageRspDTO<SupplierChemicalCoaQueryRspDTO> result = supplierChemicalCoaService
                .queryAllCoaInfo(supplierChemicalCoaQueryReqDTO);
        List<SupplierChemicalCoaQueryRspDTO> resultRecords = result.getRecords();
        List<Long> companyIds = new ArrayList<>();
        if (CollUtil.isEmpty(resultRecords)){
            return result;
        }
        resultRecords.forEach(e -> {
            companyIds.add(e.getCompanyId());
        });
        if (CollUtil.isEmpty(companyIds)){
            return result;
        }
        Map<Long, CompanyKeyFieldDTO> companyRspDTOMap = companyService.listByIds(companyIds);
        resultRecords.forEach(chemicalCoaQueryRspDTO -> {
            if (companyRspDTOMap.containsKey(chemicalCoaQueryRspDTO.getCompanyId())){
                chemicalCoaQueryRspDTO.setCompanyName(companyRspDTOMap.get(chemicalCoaQueryRspDTO.getCompanyId()).getName());
            }
        });
        return result;
    }

    @Override
    public List<SupplierChemicalCoaQueryRspDTO> queryBy(SupplierChemicalCoaQueryByReqDTO reqDTO) {
        return supplierChemicalCoaService.queryBy(reqDTO);
    }

    @Override
    public void chemicalCoaAdd(ChemicalCoaInfoReqDTO chemicalCoaInfoReqDTO) {
        supplierChemicalCoaService.chemicalCoaAdd(chemicalCoaInfoReqDTO);
    }

    @Override
    public void updateChemicalCoa(ChemicalCoaInfoReqDTO chemicalCoaInfoReqDTO) {
        supplierChemicalCoaService.updateChemicalCoa(chemicalCoaInfoReqDTO);
    }

    @Override
    public SupplierChemicalCoaQueryRspDTO queryCoaInfoById(Long chemicalCoaId) {

        SupplierChemicalCoaQueryRspDTO result = supplierChemicalCoaService
                .queryCoaInfoById(chemicalCoaId);
        if (result.getCompanyId() == null){
            return result;
        }
        CompanyQueryRspDTO companyQueryRspDTO = companyService.queryCompanyById(result.getCompanyId());
        if (companyQueryRspDTO != null){
            result.setCompanyName(companyQueryRspDTO.getName());
        }
        return result;
    }

    @Override
    public void copyAsOrderDetailSource(Long requesterId, Long chemicalCoaId, Long orderDetailId, Integer cCoaSourceType) {
        supplierChemicalCoaService.copyAsOrderDetailSource(requesterId, chemicalCoaId, orderDetailId, cCoaSourceType);
    }

    @Override
    public void resetSourceId(Long requesterId, Long chemicalCoaId) {
        supplierChemicalCoaService.resetSourceId(requesterId, chemicalCoaId);
    }

    @Override
    public void settingFields(Long requesterId, Long chemicalCoaId, ChemicalCoaInfoReqDTO onlyNonNullFeilds) {
        supplierChemicalCoaService.settingFields(requesterId, chemicalCoaId, onlyNonNullFeilds);
    }
}
